package Two;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyJFrame extends JFrame {
    private MyText nameText = new MyText("姓名",120,100,this);
    private MyText ageText = new MyText("年龄",120,200,this);
    private MyText phNumText = new MyText("电话",120,300,this);
    protected static int number = 0;
    protected static Emp[] empArray = new Emp[10];

    public MyJFrame(){
        this.setLayout(null);

        this.init();

        this.setSize(600,600);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    }

    private void init(){
        MyButton add = new MyButton("添加",100,400,this);
        MyButton show = new MyButton("显示",250,400,this);
        MyButton search = new MyButton("查找",400,400,this);


        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addOption();
            }
        });

        show.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showOption();
            }
        });

        search.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                searchOption();
            }
        });
    }

    private void addOption(){
        String errorInfo = this.check();
        if (errorInfo.length()==0){
            JOptionPane.showMessageDialog(null,"添加成功");
            Emp e = new Emp();
            String name = nameText.getText();
            String age = ageText.getText();
            String phNum = phNumText.getText();

            e.setName(name);
            e.setAge(age);
            e.setPhNum(phNum);

            empArray[number] = e;
            number++;

            if (number==empArray.length){
                this.addArray();
            }
        }else {
            JOptionPane.showMessageDialog(null,"信息有误"+errorInfo);
        }
    }

    private String check(){
        String nameCheck = "[\u4e00-\u9fa5a-zA-Z]{2,}";
        String ageCheck = "[0-9][0-9]";
        String phNumCheck = "1[3578][0-9]{9}";

        String information = "";
        if (nameText.getText().matches(nameCheck)==false){
            information += "名字必须为两位以上的字母或汉字"+"\n";
        }
        if (ageText.getText().matches(ageCheck)==false){
            information += "年龄必须为1-2位数字"+"\n";
        }
        if (phNumText.getText().matches(phNumCheck)==false){
            information += "电话必须为13、15、17、18开始的11位数"+"\n";
        }

        return information;
    }

    private void addArray(){
        Emp[] newArray = new Emp[number+10];
        for (int i=0;i<number;i++){
            newArray[i] = empArray[i];
        }
        empArray = newArray;
    }

    private void showOption(){
        String str = "";
        for (int i=0;i<number;i++){
            str += "员工姓名："+empArray[i].getName()+"   年龄："+empArray[i].getAge()+"   电话："+empArray[i].getPhNum()+"\n";
        }
        System.out.println(str);
    }

    private void searchOption(){
        int index = -1;
        String name = nameText.getText();
        for (int i=0;i<number;i++){
            if (empArray[i].getName().equals(name)){
                index = i;
            }
        }
        if (index == -1){
            JOptionPane.showMessageDialog(null,"查无此人");
        }else {
            ageText.setText(empArray[index].getAge());
            phNumText.setText(empArray[index].getPhNum());
        }
    }


    public static void main(String[] args) {
        new MyJFrame();
    }
}
